<?php

App::uses('AppController', 'Controller');

class SearchController extends AppController {

    public $helpers = array('Html');

    public function index() {
        $this->loadModel('Broker');
        $this->loadModel('Company');
        $this->loadModel('Stock');
        $this->loadModel('StockValues');
        $this->loadModel('Opinion');
        if (isset($this->request->query['search']) && trim($this->request->query['search']) != '' && (isset($this->request->query['company']) || isset($this->request->query['performer']) || isset($this->request->query['analyst']))) {
            $key = trim($this->request->query['search']);
            $this->set('keyword', $key);
            if (isset($this->request->query['company'])) {
                $this->paginate = array(
                    'conditions' => array('status' => 1, 'name LIKE ' => '%' . $key . '%'),
                    'limit' => 12,
                    'order' => array('id' => 'desc')
                );
                $rows = $this->paginate('Company');
                $this->set('rows', $rows);
                $this->set('type', 1);
            } elseif (isset($this->request->query['performer'])) {
                $this->paginate = array(
                    'conditions' => array('status' => 1, 'fullname LIKE ' => '%' . $key . '%'),
                    'limit' => 12,
                    'order' => array('id' => 'desc')
                );
                $rows = $this->paginate('Broker');
                foreach ($rows as $pk => $performer) {
                    $opinions = $this->Opinion->find('all', array('conditions' => array('broker' => $performer['Broker']['id'], 'result >' => 0)));
                    $performer['Broker']['opinions'] = $opinions;
                    $rows[$pk] = $performer;
                }

                $this->set('rows', $rows);
                $this->set('type', 2);
            } else {
                $this->paginate = array(
                    'limit' => 12,
                    'order' => array('id' => 'desc'),
                    'conditions' => array('Broker.status' => 1, 'Stock.status' => 1, 'Stock.name LIKE ' => '%' . $key . '%'),
                    'fields' => array(
                        'Stock.*', 'Broker.fullname', 'Broker.id'
                    ),
                    'recursive' => -1,
                    'joins' => array(
                        array(
                            'table' => 'brokers',
                            'type' => 'left',
                            'alias' => 'Broker',
                            'conditions' => array('Stock.broker = Broker.id')
                        )
                    )
                );
                $rows = $this->paginate('Stock');

                foreach ($rows as $bk => $stock) {
                    $stock_last_value = $this->StockValues->find('all', array('limit' => 2, 'order' => array('date desc', 'updated desc'), 'conditions' => array('stock' => $stock['Stock']['id'])));
                    $stock['Stock']['values'] = $stock_last_value;
                    $rows[$bk] = $stock;
                }


                $this->set('rows', $rows);
                $this->set('type', 3);
            }
        }
    }

}
